// 加载订单数据
async function loadOrdersData() {
    try {
        const response = await fetch(`${API_BASE_URL}/orders`, {
            method: 'GET',
            credentials: 'same-origin'
        });
        
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        
        const orders = await response.json();
        displayAllOrders(orders);
    } catch (error) {
        console.error('加载订单数据失败:', error);
        showAlert('加载订单数据失败，请刷新页面重试。', 'danger');
    }
}

// 显示所有订单数据
function displayAllOrders(orders) {
    if (!orders || orders.length === 0) {
        ordersTable.innerHTML = '<tr><td colspan="6" class="text-center">暂无订单数据</td></tr>';
        return;
    }
    
    const ordersHtml = orders.map(order => `
        <tr>
            <td>${order.orderNumber}</td>
            <td>${order.shipperInfo ? order.shipperInfo.name : '未知'}</td>
            <td>${order.recipientInfo ? order.recipientInfo.name : '未知'}</td>
            <td><span class="badge ${getOrderStatusBadgeClass(order.status)}">${formatOrderStatus(order.status)}</span></td>
            <td>${formatDate(order.creationTime)}</td>
            <td>
                <button class="btn btn-sm btn-outline-primary" onclick="viewOrderDetail('${order.orderNumber}')">查看</button>
            </td>
        </tr>
    `).join('');
    
    ordersTable.innerHTML = ordersHtml;
}

// 导出函数
window.loadOrdersData = loadOrdersData;